<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 7:43 PM
 */
declare(strict_types=1);


namespace application\module\admin\controller;


use application\module\admin\addon\Rbca as A;
use wshore\core\Response;
use wshore\throws\DatabaseException;

class auth extends admin
{

    /**
     * 页面:用户角色管理
     */
    public function userRole()
    {
        $this->doLayoutAssign();
        $this->display();
    }

    /**
     * 页面:角色权限管理
     */
    public function roleAccess()
    {

        $this->doLayoutAssign();
        $this->display();
    }


    public function roleUser()
    {

        $this->doLayoutAssign();
        $this->display();
    }

    public function accessRole()
    {

        $this->doLayoutAssign();
        $this->display();
    }

//------------------------------ 接口 ------------------------------//

    public function getUserRole(int $uid)
    {
        $addon = A::getInstance();
        try {
            $list = $addon->getUserRoles($uid);
            Response::success('OK', $list);
        } catch (DatabaseException $e) {
            Response::failure('获取列表失败:' . $e->getMessage(), []);
        }
    }

    public function addUserRole(int $uid, int $rid)
    {
        $addon = A::getInstance();
        try {
            $res = $addon->addUserRole($uid, $rid);
            if ($res) {
                Response::success('添加角色成功');
            } else {
                Response::failure('添加角色失败');
            }
        } catch (DatabaseException $e) {
            Response::failure('添加角色失败:' . $e->getMessage());
        }
    }

    public function removeUserRole(int $uid, int $rid)
    {
        $addon = A::getInstance();
        try {
            $res = $addon->removeUserRole($uid, $rid);
            if ($res) {
                Response::success('删除角色成功');
            } else {
                Response::failure('删除角色失败');
            }
        } catch (DatabaseException $e) {
            Response::failure('删除角色失败:' . $e->getMessage());
        }
    }

    public function getRoleAccess(int $rid)
    {
        $addon = A::getInstance();
        try {
            $list = $addon->getRoleAccess($rid);
            Response::success('OK', $list);
        } catch (DatabaseException $e) {
            Response::failure('获取列表失败:' . $e->getMessage(), []);
        }
    }

    public function addRoleAccess(int $rid, int $aid)
    {
        $addon = A::getInstance();
        try {
            $res = $addon->addRoleAccess($rid, $aid);
            if ($res) {
                Response::success('添加权限成功');
            } else {
                Response::failure('添加权限失败');
            }
        } catch (DatabaseException $e) {
            Response::failure('添加权限失败:' . $e->getMessage());
        }
    }

    public function removeRoleAccess(int $rid, int $aid)
    {
        $addon = A::getInstance();
        try {
            $res = $addon->removeRoleAccess($rid, $aid);
            if ($res) {
                Response::success('删除权限成功');
            } else {
                Response::failure('删除权限失败');
            }
        } catch (DatabaseException $e) {
            Response::failure('删除权限失败:' . $e->getMessage());
        }
    }

}